package com.dailyblue.java.spring.rocketmq.mq02.controller;

import com.dailyblue.java.spring.rocketmq.mq02.util.JsonResult;
import com.dailyblue.java.spring.rocketmq.mq02.util.ResultTool;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;

@RestController
@RequestMapping("/first")
public class FirstRocketMQController {
    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @GetMapping("/{body}")
    public JsonResult execute(@PathVariable("body") String body) throws Exception {
        DefaultMQProducer producer = rocketMQTemplate.getProducer();
        producer.setProducerGroup("zhuoyue01");
        producer.setNamesrvAddr("192.168.3.19:9876");
        String topic = "TopicA";
        String tags = "tagA";
        Message message = new Message(topic, tags, body.getBytes(StandardCharsets.UTF_8));
        SendResult result = producer.send(message);
        return ResultTool.success(result);
    }
}
